

def get_s_data():
    data_s = []
    with open('./static/s.txt', "r", encoding='gbk', errors='ignore') as j:
        for line in j:
            line = line[:-1]
            SNO, SNAME, STATUS, CITY = line.split("	")
            data_s.append((SNO, SNAME, STATUS, CITY))
    return data_s


def get_j_data():
    data_j = []
    with open('./static/j.txt', "rb") as j:
        for line in j:
            line = line[:-1]
            JNO, JNAME, CITY = line.decode("gbk").split("	")
            data_j.append((JNO, JNAME, CITY))
    return data_j


def get_p_data():
    data_p = []
    with open('./static/p.txt', "rb") as p:
        for line in p:
            line = line[:-1]
            PNO, PNAME, COLOR, WEIGHT = line.decode("gbk").split("	")
            data_p.append((PNO, PNAME, COLOR, WEIGHT))
    return data_p


def get_spj_data():
    data_spj = []
    with open('./static/spj.txt', "rb") as spj:
        for line in spj:
            line = line[:-1]
            SNO, PNO, JNO, QTY = line.decode("gbk").split("	")
            data_spj.append((SNO, PNO, JNO, QTY))
    return data_spj


def delete_data_row(x):
    with open('./static/s.txt', 'r', encoding='gbk') as r:
        lines = r.readlines()
    with open('./static/s.txt', 'w', encoding='gbk') as rw:
        print('2')
        print(type(x))
        for l in lines:
            if x not in l:
                rw.write(l)


def update_spj(s, p, j, qty):
    print('3')
    with open('./static/spj.txt', 'r', encoding='gbk') as r:
        line = r.readlines()
    with open('./static/spj.txt', 'w', encoding='gbk') as spj:
        print(spj)
        a = s + '	' + p + '	' + j
        for l in line:
            if a in l:
                temp = l[:-5]
                data = str(temp) + ' 	' + str(qty) + '\n'
                print(l)
                print(data)
                l = l.replace(l, data)
            spj.write(l)


# def update_spj(s, p, j, qty):
#     with open('./static/spj.txt', 'r', encoding='gbk') as r:
#         line = r.readlines()
#     with open('./static/spj.txt', 'w', encoding='gbk') as spj:
#         a = s + '	' + p + '	' + j
#         for l in line:
#             if a in l:
#                 temp = l[-4:-1]
#                 l=l.replace(temp,qty)
#                 print(l)
#                 print('yes')
#             print(l)
#             spj.write(l)


def insert_data_s(x, y, z, t):
    with open('./static/s.txt', 'a', encoding='gbk') as j:
        a = str(x) + '	' + str(y) + '	' + str(z) + '	' + str(t) + '\n'
        print(type(a))
        print('1')
        j.write(a)

def get_new_data():
    data_new = []
    with open('./static/new.txt', "r", encoding='gbk') as j:
        for line in j:
            line = line[:-1]
            SNO, PNO, JNO, QTY, SNAME, STATUS, CITY = line.split("	")
            data_new.append((SNO, PNO, JNO, QTY, SNAME, STATUS, CITY))
    return data_new


def search_s(sno):
    with open('./static/s.txt', 'r', encoding='gbk') as r1, open('./static/spj.txt') as r2:
        line1 = r1.readlines()
        line2 = r2.readlines()
    with open('./static/new.txt', 'w', encoding='gbk') as w:
        title='SNO	PNO	JNO	QTY	SNAME	STATUS	CITY'+'\n'
        w.write(title)
        for l1 in line1:
            if sno in l1:
               a=l1[2:]
               print(a)
               for l2 in line2:
                  if sno in l2:
                    l2=l2[:-1]+a
                    print(l2)
                    w.write(l2)





if __name__ == '__main__':
    search_s('S2')
